//
//  XYtest.m
//  aaaaa
//
//  Created by qianfeng on 15/8/5.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "XYtest.h"

@implementation XYtest
//1.将传入的字符串逆序
//比如输入 @"abcdef"输出@"fedcba"
+ (NSString *) niXu:(NSString *)str{
    NSMutableString * str1 = [NSMutableString string];
    for (int i = 0; i < [str length]; i ++) {
        unichar ch = [str characterAtIndex:i];
        [str1 insertString:[NSMutableString stringWithFormat:@"%c",ch] atIndex:0];
     }
    return str1;
}
//2.压缩字符串
//比如输入@"aaabbbcceaa" 输出@"3a3b2c1e2a"
+ (NSString *) yaSuo:(NSString *)str{
    NSMutableString * str1 = [[NSMutableString alloc] init];
    int count = 1;
  
    int j;
    for (int i = 0; i < [str length]; i++) {
      int flag = 0;
        unichar ch = [str characterAtIndex:i];
        for (j = i + 1; j < [str length]; j++) {
            unichar ch1 = [str characterAtIndex:j];
            if (ch != ch1) {
                i = j - 1;
                flag = 1;
                break;
            }
            count++;
          }
          [str1 appendFormat:@"%d%c",count,ch];
        if (flag == 0 && j == [str length]) {
            break;
        }
        count = 1;
    }
    return  str1;
}
//3.对字符串按字母顺序进行排序
//比如输入@"aczabczab"  输出@"aaabbcczz"
+(NSString*)sortString:(NSString*)srcStr{
    NSMutableString * str = [NSMutableString stringWithString:srcStr];
    for (int i = 0 ; i < [str length]; i++) {
        for (int j = i + 1; j < [str length]; j++) {
            unichar ch1 = [str characterAtIndex:i];
            unichar ch2 = [str characterAtIndex:j];
            if (ch1 > ch2) {
                [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch2]];
                [str replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",ch1]];
            }
        }
    }
    return str;
}


//4.将原字符串进行小写转大写，大写转小写
//比如输入 @"aBCdEfGA" 输出 @"AbcDeFga"
+(NSString*)reveserChacter:(NSString*)srcStr{
    NSMutableString * str = [[NSMutableString alloc] initWithString:srcStr];
    for (int i = 0 ; i < [str length]; i++) {
        unichar ch = [str characterAtIndex:i];
        if (isupper(ch)) {
            [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch + 32]];
        }
        if (islower(ch)) {
            [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch - 32]];
        }        
    }
    return str;
}
    

//5.替换字符串srcStr中从startKey到endKey之间的字符串//返回结果
//比如输入srcStr=@"abcdaabbccdef" startKey=@"da" endKey=@"cde" 输出 @"abbc"
+(NSString *)replaceString:(NSMutableString *)srcStr betweenStartKey:(NSString* )startKey  andEndKey:(NSString*)endKey{
    
    NSRange range1 = [srcStr rangeOfString:startKey];
    
    NSRange range2 = [srcStr rangeOfString:endKey];
    
    NSRange range = NSMakeRange(range1.location + range1.length , range2.location - range1.location - range1.length );
    
    NSString  * subStr = [srcStr substringWithRange:range];
    
    return subStr;
}







@end























